package com.silence.qhyh.webpro.mvp.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.silence.qhyh.webpro.R;
import com.silence.qhyh.webpro.mvp.ui.fragment.main.CartFragment;
import com.silence.qhyh.webpro.mvp.ui.fragment.main.GiftFragment;
import com.silence.qhyh.webpro.mvp.ui.fragment.main.HotGoodFragment;
import com.silence.qhyh.webpro.mvp.ui.fragment.main.MainFragment;
import com.silence.qhyh.webpro.mvp.ui.fragment.main.MineFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

/**
 * @author silence
 */
public class MainActivity extends BaseActivity {

    private MagicIndicator magicIndicator;
    private ViewPager viewPager;
    private String[] title = new String[]{"首页", "热卖", "礼品区", "购物车", "我的"};
    private FragmentAdapter fragmentAdapter;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        magicIndicator = findViewById(R.id.magicIndicator);
        viewPager = findViewById(R.id.viewpager);
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(fragmentAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });
        initMagicindicator();
    }

    /**
     * 初始化底部导航
     * */
    private void initMagicindicator() {
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setEnablePivotScroll(true);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return title == null ? 0 : title.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
                commonPagerTitleView.setContentView(R.layout.main_pager_title_layout);
                final TextView textView = commonPagerTitleView.findViewById(R.id.textview);
                final ImageView imageView = commonPagerTitleView.findViewById(R.id.im_view);
                textView.setText(title[index]);
                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {
                    @Override
                    public void onSelected(int index, int totalCount) {
                        textView.setTextColor(ContextCompat.getColor(context, R.color.color_EA3323));
                        if (index == 0) {
                            imageView.setImageResource(R.mipmap.main_sel_icon);
                        }
                        if (index == 1) {
                            imageView.setImageResource(R.mipmap.remai_sel_icon);
                        }
                        if (index == 2) {
                            imageView.setImageResource(R.mipmap.gift_sel_icon);
                        }
                        if (index == 3) {
                            imageView.setImageResource(R.mipmap.shopping_sel_icon);
                        }
                        if (index == 4) {
                            imageView.setImageResource(R.mipmap.mine_sel_icon);
                        }
                    }

                    @Override
                    public void onDeselected(int index, int totalCount) {
                        textView.setTextColor(ContextCompat.getColor(context, R.color.color_333333));
                        if (index == 0) {
                            imageView.setImageResource(R.mipmap.main_nor_icon);
                        }
                        if (index == 1) {
                            imageView.setImageResource(R.mipmap.remai_nor_icon);
                        }
                        if (index == 2) {
                            imageView.setImageResource(R.mipmap.gift_nor_icon);
                        }
                        if (index == 3) {
                            imageView.setImageResource(R.mipmap.shopping_nor_icon);
                        }
                        if (index == 4) {
                            imageView.setImageResource(R.mipmap.mine_nor_icon);
                        }
                    }

                    @Override
                    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
                    }

                    @Override
                    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
                    }
                });
                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });

        //绑定导航栏
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    }

    private class FragmentAdapter extends FragmentPagerAdapter {

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    MainFragment fragmentOne = new MainFragment();
                    fragment = fragmentOne;
                    break;
                case 1:
                    HotGoodFragment fragmentTwo = new HotGoodFragment();
                    fragment = fragmentTwo;
                    break;
                case 2:
                    GiftFragment fragmentThree = new GiftFragment();
                    fragment = fragmentThree;
                    break;
                case 3:
                    CartFragment fragment4 = new CartFragment();
                    fragment = fragment4;
                    break;
                case 4:
                    MineFragment fragment5 = new MineFragment();
                    fragment = fragment5;
                    break;
                default:
                    break;

            }
            return fragment;
        }

        @Override
        public int getCount() {
            return title.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }
}
